package com.example.healthmanagement.utils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;

public class JsonUtils {

    private static final JsonParser jsonParser = new JsonParser();

    public static <T> List<T> toList(String jsonString, Class<T> cls) {
        Gson gson = new Gson();
        List<T> list = new ArrayList<>();

        JsonArray array = jsonParser.parse(jsonString).getAsJsonArray();
        for (JsonElement elem : array) {
            list.add(gson.fromJson(elem, cls));
        }
        return list;
    }
}
